from collections import defaultdict
class UnionFind():
    def __init__(self, num_node):
        self.num_node = num_node
        self.parent = [i for i in range(num_node+1)]
        
    def find(self, node):
        if self.parent[node] != node:
            self.parent[node] = self.find(self.parent[node])
        return self.parent[node]
    
    def union(self, s, t):
        parent_s = self.find(s)
        parent_t = self.find(t)
        if parent_t != parent_s:
            self.parent[parent_t] = parent_s
    
    def is_same(self, node1, node2):
        return self.find(node1) == self.find(node2)
   

def is_tree_remove_edge(edge, remove_edge):
    uf = UnionFind(len(edge))
    for s, t in edge:
        if [s,t] == remove_edge:
            continue
        if uf.is_same(s, t):
            return False
        uf.union(s, t)
    return True
    


def main():
    num_node = int(input())
    
    edge = []
    edge_dict = defaultdict(list)
    
    is2 = []
    for i in range(num_node):
        s, t = map(int, input().split())
        edge.append([s, t])
        edge_dict[t].append(i)
        if len(edge_dict[t]) == 2:
            is2 = edge_dict[t]
    
    
    if len(is2) > 0:
        s,t = edge[is2[1]]
        if is_tree_remove_edge(edge, edge[is2[1]]):
            print(f'{s} {t}')
        else:
            s_move, t_move = edge[is2[0]]
            print(f'{s_move} {t_move}')
            return 
    else:
        ufmain = UnionFind(num_node)
        for i in range(num_node):
            s, t = edge[i]
            
            if ufmain.is_same(s, t):
                print(f"{s} {t}")
                return
            else:
                ufmain.union(s, t) 
    
main()